//
//  ShopDetailViewController.m
//  BabyAndBaby
//
//  Created by 范琦 on 16/7/28.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "ShopDetailViewController.h"
#import "LyScrollMenu.h"
#import "ShopViewCell.h"
#import "ShopDetailCell.h"

#import "ShopDetailModel.h"
#import "ShopMenuModel.h"
#import "ShopEditViewController.h"

@interface ShopDetailViewController ()<UIWebViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) LyScrollMenu *scroll;
@property (nonatomic, strong) NSMutableArray *viewArr;      //滚动view数组
@property (nonatomic, strong) ShopDetailModel *detailModel;      //滚动view数组
@property (nonatomic, strong) NSMutableArray *menuArr;      //上方数组
@property (nonatomic, strong) NSMutableArray *menuStrArr;      //上方数组
@property (nonatomic, strong) NSMutableArray *listArr;
@property (nonatomic, strong) NSMutableArray *listArrDetail;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ShopDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;

    [self netHandle];

}
- (void)loadView
{
    [super loadView];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _viewArr = [NSMutableArray array];
    self.title = @"商户详情";
    self.listArr = [NSMutableArray arrayWithObjects:@"商户编号:",@"商户名称:",@"联系人:",@"登陆账号(手机号):",@"详细地址:",@"店主姓名:",@"店主电话:", nil];
    [self creatTableView];
    [self menuNetHandle];
    
}
#warning 网络请求
- (void)netHandle
{
    self.listArrDetail = [NSMutableArray array];
    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"/Merchant/merchantDetails.html" paramDic:@{@"id":_str, @"token":TOKEN} RegisterApiMethod:RequestMethodGet];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        self.detailModel = [[ShopDetailModel alloc]init];
        [_detailModel setValuesForKeysWithDictionary:request.responseBody[@"result"]];
        
        [_listArrDetail addObject:[_detailModel.number stringValue]];
        [_listArrDetail addObject:_detailModel.name_merchant];
        [_listArrDetail addObject:_detailModel.contact_name];
        [_listArrDetail addObject:_detailModel.user_name];
        [_listArrDetail addObject:_detailModel.address];
        [_listArrDetail addObject:_detailModel.name];
        [_listArrDetail addObject:_detailModel.boss_phone];
        [_tableView reloadData];
        
    } failure:^(YTKBaseRequest *request) {
        
    }];

}

- (void)menuNetHandle
{
    
    _menuArr = [NSMutableArray array];
    _menuStrArr = [NSMutableArray array];

    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"/Merchant/merchantMenu.html" paramDic:@{@"token":TOKEN} RegisterApiMethod:RequestMethodGet];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        for (NSDictionary *dic in request.responseBody[@"result"]) {
            ShopMenuModel *menu = [[ShopMenuModel alloc]init];
            [menu setValuesForKeysWithDictionary:dic];
            [_menuArr addObject:menu];
            [_menuStrArr addObject:menu.name];
        }
        
        [self creatView];
        [self creatMenuScroll];

    } failure:^(YTKBaseRequest *request) {
        
    }];



}


#warning 创建所有View
- (void)creatView
{
    for (int i = 0; i < _menuArr.count; i++) {
        UIWebView *view = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, _scroll.bounds.size.height)];
        ShopMenuModel *model = _menuArr[i];
        NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"%@?uid=%@",model.url,_str]];
        NSURLRequest *request =[NSURLRequest requestWithURL:url];
        [view loadRequest:request];
        [_scroll addSubview:view];
        view.delegate = self;
        [self.viewArr addObject:view];
    }
}

- (void)creatMenuScroll
{
    [_menuStrArr insertObject:@"基本信息" atIndex:0];
    self.scroll = [[LyScrollMenu alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) ControllerViewArray:_viewArr TitleArray:_menuStrArr MaxShowTitleNum:5];
    
    
    [self.view addSubview:_scroll];
}
- (void)creatTableView
{
    
        UIView *oneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, _scroll.bounds.size.height)];
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 44 - 44 - self.navigationController.navigationBar.frame.size.height - 20) style:UITableViewStyleGrouped];
        [oneView addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = HUIBACKGROUNDCOLOR;
        [_tableView registerNib:[UINib nibWithNibName:@"ShopDetailCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        _tableView.sectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;

    
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    editBtn.frame = CGRectMake(0, _tableView.bounds.size.height, KSCREENWIDTH, 44);
    [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    editBtn.tintColor = [UIColor whiteColor];
    editBtn.backgroundColor = BACKGROUNDCOLOR;
    [[editBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        ShopEditViewController *edit = [[ShopEditViewController alloc]init];
        edit.shopDetailModel = _detailModel;
        edit.str = _str;
        edit.menuArr = _menuArr;
        edit.menuStrArr = _menuStrArr;
        edit.listArrDetail = _listArrDetail;
        [self.navigationController pushViewController:edit animated:YES];
    }];
    [oneView addSubview:editBtn];
    [_scroll addSubview:oneView];
    [_viewArr addObject:oneView];
}
#warning tableView代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 43;
    }
    return 58;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 5;
    }
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15 + 33;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 33 + 15)];
    view.backgroundColor = HUIBACKGROUNDCOLOR;
    UIView *back = [[UIView alloc]initWithFrame:CGRectMake(0, 15, KSCREENWIDTH, 33)];
    back.backgroundColor = [UIColor whiteColor];
    [view addSubview:back];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 12, KSCREENWIDTH, 21)];
    label.backgroundColor = [UIColor whiteColor];
    if (section == 0) {
        label.text = @"基本信息";
    }else
    {
        label.text = @"验证信息";
    }
    label.textColor = [UIColor colorWithRed:76 / 255.0 green:217 / 255.0 blue:192 / 255.0 alpha:1];
    label.font = [UIFont systemFontOfSize:16];
    [back addSubview:label];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textField.enabled = NO;
    if (indexPath.section == 0 && _listArrDetail.count != 0) {
        cell.textField.text = [NSString stringWithFormat:@"%@ %@", _listArr[indexPath.row], _listArrDetail[indexPath.row]];
 
        
        
    }else if (indexPath.section == 1 && _listArrDetail.count != 0){
    cell.textField.text = [NSString stringWithFormat:@"%@ %@", _listArr[indexPath.row + 5], _listArrDetail[indexPath.row + 5]];
    }


    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
